﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DBExperment
{
    public partial class BookListPanel : UserControl
    {
        public DataTable BookList = new DataTable();
        private List<Book> books = new List<Book>();

        public BookListPanel()
        {
            InitializeComponent();
            FillBookTable("SELECT * From tb_BookList");
        }
                
        public void FillBookTable(string sql)
        {
            BookList = new DataTable();
            BookList.Columns.Add("选中", typeof(bool));
            BookList.Columns.Add("编号");
            BookList.Columns.Add("书名");
            BookList.Columns.Add("状态");
            BookList.Columns.Add("学校");
            BookList.Columns.Add("图书馆");            
            BookList.Columns.Add("类别");
            books = Data.FindBookListBy(sql);
            object[] values = new object[7];

            for (int i = 0; i < books.Count; i++)
            {
                values[0] = false;
                values[1] = books[i].ID;
                values[2] = books[i].BName;
                values[3] = books[i].Status;
                values[4] = books[i].School;
                values[5] = books[i].Liberty;
                values[6] = books[i].Type;
                BookList.Rows.Add(values);
            }

            DGV_Book.DataSource = BookList;
            DGV_Book.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            DGV_Book.Columns[0].FillWeight = 10;
            DGV_Book.Columns[1].FillWeight = 20;
            DGV_Book.Columns[2].FillWeight = 20;
            DGV_Book.Columns[3].FillWeight = 20;
            DGV_Book.Columns[4].FillWeight = 20;
            DGV_Book.Columns[5].FillWeight = 20;
            DGV_Book.Columns[6].FillWeight = 20;
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            AddBookForm addBookForm=new AddBookForm();
            addBookForm.ShowDialog();
            FillBookTable("SELECT * From tb_BookList");
        }

        private void btn_Find_Click(object sender, EventArgs e)
        {
            string sql = "SELECT * From tb_BookList WHERE";
            if (rad_BName.Checked)
            {
                sql = "SELECT * From tb_BookList WHERE BID=(SELECT BID FROM tb_Book WHERE BName=" + "'" + txt_Input.Text +"')";
            }
            if (rad_Type.Checked)
            {
                sql = "SELECT * From tb_BookList WHERE Type=" + "'" + txt_Input.Text + "'";
            }
            if (rad_Library.Checked)
            {
                sql = "SELECT * From tb_BookList WHERE LID=(SELECT LID FROM tb_Library WHERE LName=" + "'" + txt_Input.Text + "')";
            }
            if (rad_School.Checked)
            {
                sql = "SELECT * From tb_BookList WHERE SID=(SELECT SID FROM tb_School WHERE SName=" + "'" + txt_Input.Text + "')";
            }
            FillBookTable(sql);
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(MessageBox.Show("确定删除记录", "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)))
            {
                for (int i = 0; i < BookList.Rows.Count; i++)
                {
                    if ((bool)BookList.Rows[i][0])
                    {
                        //MessageBox.Show("DELETE * FROM tb_Book WHERE BID=" + BookTable.Rows[i][1]);
                        Data.AddData("DELETE * FROM tb_BookList WHERE BookID=" + BookList.Rows[i][1]);
                        BookList.Rows.Remove(BookList.Rows[i]);

                    }
                }
            }
        }

        private void DGV_Book_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int RIndex = e.RowIndex;
            if (RIndex > -1)
            {
                BookList.Rows[RIndex][0] = !Convert.ToBoolean(BookList.Rows[RIndex][0]);
            }
        }

        private void btn_All_Click(object sender, EventArgs e)
        {
            FillBookTable("SELECT * From tb_BookList");
        }

    }
}
